package com.zyzd.baseretrofit.net;

import com.zyzd.baseretrofit.bean.remote.ApiResultBean;
import com.zyzd.commontools.cache.SpUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

/**
 * Created by LZY on 2017/6/26.
 */

public class RetrofitHelper {
    private static String baseUrl = "http://xingju.mstring.cn/xingjuapp-front/";
    private static Retrofit retrofit;
    private static Api api;

    public static Retrofit getRetrofit() {
        if (retrofit == null) {

            OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request()
                            .newBuilder()
                            .addHeader("token", SpUtil.getString("token", ""))
                            .build();
                    return chain.proceed(request);
                }
            }).build();

            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .client(httpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();

        }
        return retrofit;
    }

    public static Api getApi() {
        if (api == null) {
            api = getRetrofit().create(Api.class);
        }
        return api;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    public static final String FILENAME = "uploadimg";

    private static String baseUploadImageUrl = "http://img.xingjuapp.com/upload/";
    private static Retrofit uploadRetrofit;
    private static UploadApi uploadApi;

    public static Retrofit getUploadRetrofit() {
        if (uploadRetrofit == null) {

            OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request()
                            .newBuilder()
                            .addHeader("token", SpUtil.getString("token", ""))
                            .build();
                    return chain.proceed(request);
                }
            }).build();

            uploadRetrofit = new Retrofit.Builder()
                    .baseUrl(baseUploadImageUrl)
                    .client(httpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
        }
        return uploadRetrofit;
    }

    public static UploadApi getUploadApi() {
        if (uploadApi == null) {
            uploadApi = getUploadRetrofit().create(UploadApi.class);
        }
        return uploadApi;
    }

    public static Observable<ApiResultBean<ArrayList<String>>> getUploadImageObservable(ArrayList<File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MEDIA_TYPE_PNG, file);
            builder.addFormDataPart(FILENAME, file.getName(), requestBody);
        }
        return getUploadApi().uploadFile(builder.build());
    }
}
